package com.bawei.module_main.ui

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.view.CountdownCircleView
import com.bawei.lib_storage.AppMMKV
import com.bawei.module_main.R
import com.bawei.module_main.databinding.ActivitySplashBinding
import com.bawei.module_main.dialog.MessageNoticeDialog

/**
 *author：yjk
 *description:
 *date：2025/6/10
*/

@Route(path = Router.SPLASH_PAGE)
class SplashActivity : BaseActivity<BaseViewModel,ActivitySplashBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_splash

    override fun initView() {
        super.initView()

        //渐变背景色
        backgroundColor()
        //倒计时结束
        mBinding.countView.setOnCountdownFinishListener(object : CountdownCircleView.OnCountdownFinishListener {
            override fun onCountdownFinished() {
                jump()
            }
        })
        //点击跳过
        mBinding.jump.setOnClickListener({
            jump()
            mBinding.countView.cancel()
            mBinding.countView.currentNumber=0
        })

        //4:弹出对话框
        if (!AppMMKV.splashAgreement){
            mBinding.countView.cancel()
            val colorMain = Color.RED
            val userAgreement = "《用户协议》"
            val privacyPolicy = "《隐私条款》"
            val content =
                "FCC智慧园区非常尊重用户个人的信息保护，我们依据最新的监管要求特向您说明如下：\n1.在使用 APP过程中，我们有可能会收集、使用设备标识信息用于推送车辆提醒消息。\n2.我们会一直采取严格的安全技术保护您的个人信息安全。\n3.未经您的同意，我们不会以协议约定之外的形式和渠道获取、共享或使用您的个人信息。\n您可通过阅读完整的${userAgreement}和${privacyPolicy}来了解详细信息。请您充分阅读并理解以上协议，点击同意按钮，即表示您已同意上述协议及以上约定。"
            val spannableStringBuilder = SpannableStringBuilder(content)
            val userAgreementStart = content.indexOf(userAgreement)
            val userAgreementEnd = userAgreementStart + userAgreement.length
            val privacyPolicyStart = content.indexOf(privacyPolicy)
            val privacyPolicyEnd = privacyPolicyStart + privacyPolicy.length

            spannableStringBuilder.setSpan(object : ClickableSpan() {
                override fun onClick(p0: View) {
//                    CommonWebActivity.jumpUserAgreement()
//                    showToast("点击了用户")
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.color = colorMain
                    ds.isUnderlineText = false
                }

            }, userAgreementStart, userAgreementEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)

            spannableStringBuilder.setSpan(object : ClickableSpan() {
                override fun onClick(p0: View) {
//                    CommonWebActivity.jumpPrivacyAgreement()
//                    showToast("点击了隐私")
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.color = colorMain
                    ds.isUnderlineText = false
                }

            }, privacyPolicyStart, privacyPolicyEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

            MessageNoticeDialog(
                title = "温馨提示",
                message = spannableStringBuilder,
                alignment = Gravity.START,
                onConfirmClick = {
                    AppMMKV.splashAgreement = true
                    mBinding.countView.startCountdown()
                },
                onCancelClick = {
                    finish()
                },
                confirmText = "同意",
                cancelText = "不同意",
                cancelable = false
            ).show(supportFragmentManager)
        }
    }
    fun backgroundColor() {
        val gradientDrawable = GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM, // 从上到下
            intArrayOf(
                Color.parseColor("#4A90E2"),
                Color.parseColor("#50E3C2")
            )
        )
        // 应用到View
        mBinding.main.background = gradientDrawable
        // 如果是Activity全屏背景
        window.setBackgroundDrawable(gradientDrawable)
    }
    fun jump(){
        ARouter.getInstance().build(Router.GUIDE_PAGE).navigation()
    }

}